package com.example.smartall.fragment.JobHunting;

import android.content.Intent;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;

import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.example.smartall.R;
import com.example.smartall.api.Api;
import com.example.smartall.api.ApiConfig;
import com.example.smartall.api.NetCallBack;
import com.example.smartall.entity.JobQueryResumeBean;
import com.example.smartall.fragment.base.BaseFragment;
import com.example.smartall.ui.JobResumeActivity;
import com.example.smartall.utils.Constant;
import com.example.smartall.utils.SPUtils;
import com.google.gson.Gson;

/**
 * @Author: abbas
 * @CreateDate: 2022/2/6
 * @Description: 个人简历页面，实现用户个人简历的管理，
 * 包括查询、新增、编辑等。
 * 个人简历包括用户基本信息、求职信息两部分。
 * 基本信息包括用户账号、用户昵称、邮件、电话号码、性别、头像等，
 * 求职信息包括工作经验、最高学历、现居住地、期望职位、薪资、自我描述、教育经历、工作经历等。
 * <p>
 * 看不懂文档是我智商问题吗
 */
public class PersonalResumeFragment extends BaseFragment {

    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";

    private String mParam1;
    private String mParam2;

    private ImageView img;
    private TextView name;
    private TextView content;
    private TextView tvPref;
    private Button btChange;

    private boolean hasResume = false;

    private JobQueryResumeBean.DataBean queryResumeBean;

    private MHandler handler;

    public PersonalResumeFragment() {
    }

    public static PersonalResumeFragment newInstance(String param1, String param2) {
        PersonalResumeFragment fragment = new PersonalResumeFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        handler = new MHandler();

        View view = inflater.inflate(R.layout.fragment_personal_resume, container, false);
        init(view);
        initEvent();
        getResume();
        return view;
    }

    @Override
    public void onResume() {
        getResume();
        super.onResume();
    }

    private void init(View view) {
        img = (ImageView) view.findViewById(R.id.img);
        name = (TextView) view.findViewById(R.id.name);
        content = (TextView) view.findViewById(R.id.content);
        tvPref = (TextView) view.findViewById(R.id.tv_perf);
        btChange = (Button) view.findViewById(R.id.bt_change);

        try {
            Glide.with(getActivity()).load(ApiConfig.BASE_URL + new SPUtils(getActivity(), Constant.SP_USER_FILE_NAME).getString(Constant.USER_AVATAR)).into(img);
        } catch (Exception e) {
            Log.e("捕获到异常", "PersonalResumeFragment");
        }
        //获取个人信息
        name.setText(SPUtils.config(getActivity(), Constant.SP_USER_FILE_NAME).getString(Constant.USER_NAME));
        content.setText(SPUtils.config(getActivity(), Constant.SP_USER_FILE_NAME).getString(Constant.USER_EMAIL) +
                "\n" + SPUtils.config(getActivity(), Constant.SP_USER_FILE_NAME).getString(Constant.USER_PHONENUMBER) +
                "\n" + (SPUtils.config(getActivity(), Constant.SP_USER_FILE_NAME).getString(Constant.USER_SEX) == "0" ? "男" : "女") +
                "\n" + SPUtils.config(getActivity(), Constant.SP_USER_FILE_NAME).getString(Constant.USER_ID));
    }

    private void initEvent() {
        btChange.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (hasResume){
                    Intent intent = new Intent(getActivity(),JobResumeActivity.class);
                    intent.putExtra("data",queryResumeBean);
                    startActivity(intent);
                }else {
                    migrateTo(JobResumeActivity.class);
                }
            }
        });
    }

    /**
     * 获取简历
     */
    private void getResume() {
        //刚刚写好的get加header 额外多一个方法，无奈之举，应该集成在一起
        Api.config(ApiConfig.JOB_RESUME, null).getRequest(getActivity(), new NetCallBack() {
            @Override
            public void onSuccess(String res) {
                //可能没有简历
                JobQueryResumeBean bean = new Gson().fromJson(res, JobQueryResumeBean.class);
                if (bean != null && bean.getCode() == 200 && bean.getData() != null) {
                    queryResumeBean = bean.getData();
                    handler.sendEmptyMessage(0x01);
                }
            }

            @Override
            public void onFailure(Exception e) {

            }
        }, "" + SPUtils.config(getActivity(), Constant.SP_USER_FILE_NAME).getString(Constant.USER_ID));
    }

    class MHandler extends Handler {
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 0x01:
                    btChange.setText("更改");
                    hasResume = true;
                    tvPref.setText("最高学历：" + (queryResumeBean.getMostEducation() == "" ? "无" : queryResumeBean.getMostEducation()) +
                            "\n" + "教育经历：" + (queryResumeBean.getEducation() == "" ? "无" : queryResumeBean.getEducation()) +
                            "\n" + "现居：" + (queryResumeBean.getAddress() == "" ? "无" : queryResumeBean.getAddress()) +
                            "\n" + "工作经验：" + (queryResumeBean.getExperience() == "" ? "无" : queryResumeBean.getExperience()) +
                            "\n" + "自我介绍：" + (queryResumeBean.getIndividualResume() == "" ? "无" : queryResumeBean.getIndividualResume()) +
                            "\n" + "期望薪资：" + (queryResumeBean.getMoney() == "" ? "无" : queryResumeBean.getMoney()) +
                            "\n" + "期望岗位ID：" + (queryResumeBean.getPositionId() == 0 ? "无" : queryResumeBean.getPositionId()));
                    break;
            }
        }
    }

}